using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using CommonBehaviors.Actions;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using TreeSharp;


namespace FelMaster.Classes.Rogue
{
    class Assasination : TalentBase
    {

        public override string Name
        {
            get { return "Assasination V3.0"; }
        }

        public override string KeySpell
        {
            get { return "Mutilate"; }
        }

        public override string Help
        {
            get
            {
                return
                    "   Focus a target to use Tricks of the Trade\n" +
                    "   Standard Assasination Rotation\n" +
                    "   No Burst Rotation\n" +
                    "-----------------------------------------";
            }
        }

        private static bool WillEnergyCap
        {
            get { return Lua.GetReturnVal<float>("return GetPowerRegen()", 1) > 18f; }
        }


        public bool IsBehind(WoWUnit target)
        {
            return WoWMathHelper.IsBehind(StyxWoW.Me.Location, target.Location, target.Rotation, (float)Math.PI * 5 / 6);
        }

        public override Composite SingleRotation
        {
            get
            {
                return new PrioritySelector(
                   new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                   SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                   SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                   SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                   SC.CastSpell("Berserking", a => true, "Berserking"),
                   SC.CastSpell("Vanish", a => !SC.PlayerHasBuff("Overkill"), "Vanish"),
                   new Decorator(
                       ret => SC.PlayerHasBuff("Vanish") || SC.PlayerHasBuff("Stealth"),
                       new PrioritySelector(
                           SC.CastSpell("Garrote", a => true, "Garrote")
                       )
                   ),
                   SC.RunMacroText("/cast [@focus] Tricks of the Trade", a => SC.CanCast("Tricks of the Trade") && StyxWoW.Me.FocusedUnitGuid != 0 && !StyxWoW.Me.FocusedUnit.Dead && StyxWoW.Me.FocusedUnit.Attackable && StyxWoW.Me.FocusedUnit.InLineOfSight && !StyxWoW.Me.FocusedUnit.Mounted, "Tricks of the Trade"),
                   SC.UseTrinket(69001, a => SC.TargetHasDebuff("Vendetta") || SC.SpellCooldown("Vendetta") > 45, "Popping Ancient Petrified Seed!"),
                   SC.CastSpell("Vendetta", a => SC.PlayerHasBuff("Overkill") || (SC.SpellCooldown("Vanish") > 60 || SC.CanCast("Vanish")), "Vendetta"),
                   SC.CastSpell("Slice and Dice", a => !SC.PlayerHasBuff("Slice and Dice"), "Slice and Dice"),
                   SC.CastAreaSpell("Fan of Knives", 12, false, 8, 0.0, 0.0, ret => true, "Fan of Knives"),
                   SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && SC.PlayerBuffTimeLeft("Slice and Dice") > 6, "Rupture"),
                   SC.CastSpell("Cold Blood", a => Me.ComboPoints >= 4 && SC.TargetDebuffTimeLeft("Rupture") > 2 && SC.PlayerHasBuff("Slice and Dice"), "Cold Blood"),
                   SC.CastSpell("Envenom", a => Me.ComboPoints >= 4 && !SC.PlayerHasBuff("Envenom"), "Envenom"),
                   SC.CastSpell("Envenom", a => Me.ComboPoints >= 4 && Me.CurrentEnergy > 80, "Envenom"),
                   SC.CastSpell("Envenom", a => Me.ComboPoints >= 2 && SC.PlayerBuffTimeLeft("Slice and Dice") < 3, "Envenom"),
                   SC.CastSpell("Mutilate", a => Me.ComboPoints < 4 && Me.CurrentTarget.HealthPercent < 35 && (!IsBehind(Me.CurrentTarget) || WillEnergyCap), "Mutilate"),
                   SC.CastSpell("Backstab", a => Me.ComboPoints < 5 && IsBehind(Me.CurrentTarget) && !WillEnergyCap && Me.CurrentTarget.HealthPercent < 35, "Backstab"),
                   SC.CastSpell("Mutilate", a => Me.ComboPoints < 4 && Me.CurrentTarget.HealthPercent > 35, "Mutilate"),
                   SC.CastSpell("Arcane Torrent", a => Me.CurrentEnergy < 40, "Arcane Torrent")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastBuff("Stealth", a => !Me.Mounted, "Stealth")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}